import { Injectable, Inject } from '@nestjs/common';
import { InjectRedis } from '@nestjs-modules/ioredis';
import Redis from 'ioredis';

// 添加缓存策略常量
const CACHE_TTL = {
  SHORT: 300, // 5分钟
  MEDIUM: 1800, // 30分钟
  LONG: 3600, // 1小时
  VERY_LONG: 86400, // 24小时
};
@Injectable()
export class RedisService {
  constructor(@InjectRedis() private readonly redis: Redis) {}

  // 移除不必要的async，直接返回Promise
  set(
    key: string,
    value: string,
    ttl?: number,
    strategy: 'short' | 'medium' | 'long' | 'very_long' | null = null,
  ): Promise<any> {
    let finalTtl = ttl;
    if (!ttl && strategy) {
      finalTtl = CACHE_TTL[strategy.toUpperCase()];
    } else if (!ttl) {
      finalTtl = 3600;
    }
    return this.redis.setex(key, finalTtl, value);
  }
  get(key: string): Promise<string | null> {
    return this.redis.get(key);
  }

  delete(key: string): Promise<any> {
    return this.redis.del(key);
  }

  // 添加批量操作方法
  mget(keys: string[]): Promise<(string | null)[]> {
    return this.redis.mget(...keys);
  }

  // 增加缓存TTL配置项
  setWithCustomTtl(key: string, value: string, ttl: number): Promise<any> {
    return this.redis.setex(key, ttl, value);
  }
}
